/*
 * MainFrame.java
 *
 * Created on March 7, 2009, 7:30 AM
 */

package com.pdjs.code.utils.uptranslit.gui;

import com.pdjs.code.utils.uptranslit.core.FeedHelper;
import java.util.StringTokenizer;

/**
 *
 * @author  niranjan
 */
public class MainFrame extends javax.swing.JFrame {
    
    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        source_textarea = new javax.swing.JTextArea();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        target_textarea = new javax.swing.JTextArea();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("A Reverse Transliterator v1.1");

        source_textarea.setColumns(20);
        source_textarea.setFont(new java.awt.Font("Arial", 0, 11));
        source_textarea.setRows(5);
        source_textarea.setToolTipText("Paste only ashaar lines here");
        source_textarea.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jScrollPane1.setViewportView(source_textarea);

        jLabel1.setText("Paste the ghazal below");

        target_textarea.setColumns(20);
        target_textarea.setEditable(false);
        target_textarea.setFont(target_textarea.getFont().deriveFont(target_textarea.getFont().getSize()+1f));
        target_textarea.setRows(5);
        target_textarea.setToolTipText("Use Crtl-C to copy");
        target_textarea.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        target_textarea.setSelectionColor(new java.awt.Color(255, 153, 255));
        jScrollPane2.setViewportView(target_textarea);

        jLabel2.setText("Reverse transliterated");

        jButton1.setText("Reverse Transliterate");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(120, 120, 120)
                        .addComponent(jButton1))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    parsesource();
}//GEN-LAST:event_jButton1ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea source_textarea;
    private javax.swing.JTextArea target_textarea;
    // End of variables declaration//GEN-END:variables

    private void parsesource() {
        int linecount = source_textarea.getLineCount();
        StringBuffer sbuff = new StringBuffer();
        if (linecount != 0) {
          String source_str = source_textarea.getText();
          StringTokenizer stT = new StringTokenizer(source_str,System.getProperty("line.separator"));
          int parsedLine_cnt=0;
          //parse each line
          while(stT.hasMoreTokens()) {
              String token = stT.nextToken();
              if (token!=null && !"".equals(token.trim()) ) {
                FeedHelper helper = FeedHelper.getInstance();
                sbuff.append(helper.transliterateLineString(token));
                sbuff.append(System.getProperty("line.separator"));
                parsedLine_cnt++;
                if (parsedLine_cnt%2 == 0 ) {
                    sbuff.append(System.getProperty("line.separator"));
                }                    
              } else {
                  sbuff.append(System.getProperty("line.separator"));
              }
          }
          target_textarea.setText(sbuff.toString());
        } 
    }
    
}
